package com.yangoxix.lovegoing.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yangoxix.lovegoing.mapper.ActiticyMapper;
import com.yangoxix.lovegoing.mapper.ImageMapper;
import com.yangoxix.lovegoing.mapper.UserInActivityMapper;
import com.yangoxix.lovegoing.page.ActivityDetail;
import com.yangoxix.lovegoing.page.ActivityPage;
import com.yangoxix.lovegoing.pojo.Activity;
import com.yangoxix.lovegoing.pojo.ActivityPageQuery;
import com.yangoxix.lovegoing.pojo.Image;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j

public class ActivityService {
    private final ImageMapper imageMapper;
    private final ActiticyMapper activityMapper;
    private final UserInActivityMapper userInActivityMapper;
    private static final int VALID_DETAIL_LENGTH = 25;

    public ActivityService(ImageMapper imageMapper, ActiticyMapper activityMapper, UserInActivityMapper userInActivityMapper) {
        this.imageMapper = imageMapper;
        this.activityMapper = activityMapper;
        this.userInActivityMapper = userInActivityMapper;
    }

    // 获取全部有用的信息
    public ArrayList<ActivityPage> buildAllActivityPage() {

//       从数据库获取数据
        Activity activityMapper = new Activity();

//      获取activity的数据
        List<Activity> as = activityMapper.selectAll();

        ArrayList<ActivityPage> Pages = new ArrayList<>();

        for (Activity a : as) {
//      通过名字获取img

            ActivityPage activityPage = new ActivityPage();

            String activityname = a.getActivityname();

            Image image = new Image();

            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();

            imageQueryWrapper.eq("activityname", activityname);

            List<Image> images = image.selectList(imageQueryWrapper);


            //把全部的imgurl放入page中的list
            ArrayList<String> imagesList = new ArrayList<>();
            for (Image imagef : images) {
                imagesList.add(imagef.getImgurl());
            }
            activityPage.setImagePath(imagesList);


            String detail = a.getDetail();
//            判断是否有超过25个字如果有对其进行截取
            if (detail.length() > 25) {
                //截取前25个字符
                activityPage.setDetailCout(detail.substring(0, 25));
            } else {
                activityPage.setDetailCout(detail);
            }

//            设置标题
            activityPage.setActivityname(activityname);


            Pages.add(activityPage);
        }
//    对数据进行封装
//    返回pagebean
        return Pages;
    }

    // 根据id获取活动详情
    public ActivityDetail getDetailById(String name) {

        Activity activity = new Activity();

        QueryWrapper<Activity> activityQueryWrapper = new QueryWrapper<>();

        activityQueryWrapper.eq("activityname", name);

        Activity one = activity.selectOne(activityQueryWrapper);

        Image image = new Image();

        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();

        imageQueryWrapper.eq("activityname", name);

        List<Image> images = image.selectList(imageQueryWrapper);

        ActivityDetail activityDetail = new ActivityDetail();

        activityDetail.setActivity(one);

        //把全部的imgurl放入page中的list
        ArrayList<String> imagesList = new ArrayList<>();
        for (Image imagef : images) {
            imagesList.add(imagef.getImgurl());
        }
        activityDetail.setImagePath(imagesList);

        return activityDetail;

    }

    public List<ActivityPage> activityPagesViaUsername(String username) {
        Optional<List<ActivityPageQuery>> pageQueries =
                Optional.ofNullable(activityMapper.selectActivityPagesViaUsername(username));
        return pageQueries.stream()
                .flatMap(Collection::stream)
                .map(ActivityService::asActivityPage)
                .collect(Collectors.toList());
    }

    private static ActivityPage asActivityPage(ActivityPageQuery pageQuery) {
        List<String> urls = Arrays.stream((pageQuery.getUrls() == null ? "" : pageQuery.getUrls()) .split(","))
                .filter(Strings::isNotBlank)
                .collect(Collectors.toList());
        return new ActivityPage(urls, pageQuery.getName(), pageQuery.getDetail());
    }

    public void deleteAct(String act) {
        Activity dao = new Activity();
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("activityname", act);
        Integer integer = dao.selectCount(wrapper);
        if (integer == 1) {
            ImageService imageService = new ImageService();
            Image imageDao = new Image();
            QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
            imageQueryWrapper.eq("activityname", act);
            List<Image> images = imageDao.selectList(imageQueryWrapper);
            for (Image e : images) {
                String imgurl = e.getImgurl();
                boolean b = imageService.deleteImage(imgurl);
                if (b) {
                    System.out.println(e.getActivityname() + "====>>>>>" + e.getImgurl() + "   已被成功删除");
                } else {
                    System.out.println(e.getActivityname() + "====>>>>>" + e.getImgurl() + "   删除失败");
                }
            }
            imageDao.delete(imageQueryWrapper);
            boolean b = dao.deleteById(act);
            if (b) {
                System.out.println("activity info is deleted");
            } else {
                System.out.println("activity info deleted fail!!!");
            }

        }



    }
}

